package com.atguigu.lease.common.sms;

// 包不要导错
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author GQS @Date 2024/9/4/18:26 @Description
 */
@Configuration
@EnableConfigurationProperties(AliyunSMSProperties.class)
@ConditionalOnProperty(prefix = "aliyun.sms", name = "endpoint") // 在配置文件中，前缀为aliyun.sms的属性中，名为endpoint的属性必须存在
public class AliyunSMSConfiguration {

  @Autowired private AliyunSMSProperties properties;

  @Bean
  public Client smsClient() {
    Config config = new Config();
    config.setAccessKeyId(properties.getAccessKeyId());
    config.setAccessKeySecret(properties.getAccessKeySecret());
    config.setEndpoint(properties.getEndpoint());
    try {
      return new Client(config);
    } catch (Exception e) {
      throw new StgyException(ResultCodeEnum.SERVICE_ERROR);
    }
  }
}
